package com.winway.sixbook.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.winway.sixbook.R;
import com.tritrees.easygridview.widget.AllEasyGridView;
import com.tritrees.easygridview.widget.EasyGridView;
import com.tritrees.easygridview.widget.EasyGridView.IHandleGridViewWithChild;
import com.tritrees.easygridview.widget.ImageAdapter;

public class AddImageWithListViewActivity extends Activity implements IHandleGridViewWithChild
{
	private static final String strTag = "AddImageWithListViewActivity";
	List<ArrayList<HashMap<String, Object>>> lists;
	ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_image_with_listview);
		initView();
		
	}
	
	private void initView()
	{
		listView = (ListView)findViewById(R.id.lv_add_image);
		
		listView.setAdapter(new MyAdapter(this, getData()));
	}
	
	private List<ArrayList<HashMap<String, Object>>> getData()
	{
		lists = new ArrayList<ArrayList<HashMap<String,Object>>>();
		
			ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
			HashMap<String,Object> map;
			Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_usericon_1);
			for(int j=0; j < 7; j++){
				map = new HashMap<String, Object>();
				map.put(EasyGridView.IMAGE, bmp);
				map.put(EasyGridView.TEXT, "娜美");
				data.add(map);
			}
			lists.add(data);
		
		return lists;
	}
	
	@Override
	public void addImage(int childPosition)
	{
		Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_usericon_1);
		HashMap<String,Object> map = new HashMap<String, Object>();
		map.put(EasyGridView.IMAGE, bmp);
		map.put(EasyGridView.TEXT, "娜美");
		lists.get(childPosition).add(map);	
	}
	@Override
	public void delImage(int childPosition, int position)
	{
		lists.get(childPosition).remove(position);
	}
	@Override
	public void queryImage(int childPosition, int position)
	{
		
	}
	
	class MyAdapter extends BaseAdapter
	{
		private List<ArrayList<HashMap<String, Object>>> lists;
		private Context context;

		public MyAdapter(Context context, List<ArrayList<HashMap<String, Object>>> lists)
		{
			this.context = context;
			this.lists = lists;
		}

		@Override
		public int getCount()
		{
			return lists.size();
		}

		@Override
		public Object getItem(int position)
		{
			return lists.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			if(convertView == null)
			{
				convertView = View.inflate(context, R.layout.activity_all_image, null);
				AllEasyGridView allEasyGridView = (AllEasyGridView)convertView.findViewById(R.id.my_grid);
				ImageAdapter adapter = new ImageAdapter(context);
				allEasyGridView.initData(true, lists.get(position), adapter);
				allEasyGridView.setHandleGridViewWithChild(position, AddImageWithListViewActivity.this);
				
			}
			return convertView;
		}
		
	}
	
}
